package com.zhaolixiang.slidetabview

import android.content.Context
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent

/**
 * Created by Administrator on 2018/1/9.
 */
class MyViewPager:ViewPager{
    constructor(context: Context):super(context)
    constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet)
    private var onTouchEventListener:OnTouchEventListener?=null
    //滑动的x坐标值
    private var perviousX = 0f
    /**
     * 滚动方向：direction
     */
    enum class Direction {
        LEFT, RIGHT, NON
    }

    //初始化，未设置
    private var direction = Direction.NON

    public  fun setOnTouchEventListener(l: OnTouchEventListener) {
        onTouchEventListener=l
        //scrollBy(123,123)
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        Log.e("话单位置","${ev.x}--${ev.y}+onTouchEventListener:$onTouchEventListener")
        if(onTouchEventListener!=null){
            onTouchEventListener!!.onTouchEvent(ev)
        }
        return super.onTouchEvent(ev)
    }
    public fun deleteOnToucheEvent(ev: MotionEvent,MinValue:Int,currentPosition:Int){

        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                //按下开始滑动
                perviousX = ev.x
            }
            MotionEvent.ACTION_MOVE -> {
                //触摸移动中
                if (ev.x > perviousX) {
                    //想右移动
                    direction = Direction.RIGHT
                } else if (ev.x < perviousX) {
                    //向左移动
                    direction = Direction.LEFT
                } else {
                    direction = Direction.NON
                }

                val yidong=-((ev.x-perviousX)*ForScreen.getWindowsWidth(context)/MinValue).toInt()
                var last:Int=yidong/ForScreen.getWindowsWidth(context)
                Log.e("ViewPage处理阶段","${ev.action}--${ev.x}--${ev.y}--$yidong--$last--$currentPosition")
                if(Math.abs(yidong)>1){
                    if(Math.abs(last)>0){
                        //说明跨屏幕了
                        currentItem=currentPosition+last
                        scrollBy(yidong-last*ForScreen.getWindowsWidth(context),0)
                    }else{
                        scrollBy(yidong,0)
                    }
                    perviousX = ev.x
                }

            }
            MotionEvent.ACTION_UP -> {
                //手指离开时
            }
        }
    }

}